/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.UserProfileDAOServices;
import model.enetities.UserProfile;
import org.omg.CORBA.PUBLIC_MEMBER;

/**
 *
 * @author Ku81
 */
public class UserProfileDAO implements UserProfileDAOServices {

    private static UserProfileDAO userProfile;
    private Connection conn;
    private Statement st;
    private ResultSet rs;

    public static UserProfileDAO getInstance() {
        if (userProfile == null) {
            userProfile = new UserProfileDAO();
        }
        return userProfile;

    }
    
    
    

    @Override
    public boolean create(UserProfile userProfile) {
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "INSERT INTO tblUserProfile VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? ,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, userProfile.getFullName());
            pstmt.setDate(2, userProfile.getDateOfBirth());
            pstmt.setBoolean(3, userProfile.isGender());
            pstmt.setString(4, userProfile.getHomeTown());
            pstmt.setString(5, userProfile.getSchool());
            pstmt.setString(6, userProfile.getMobile());
            pstmt.setString(7, userProfile.getEmail());
            pstmt.setString(8, userProfile.getStrengths());
            pstmt.setString(9, userProfile.getLanguages());
            pstmt.setString(10, userProfile.getImage());
            pstmt.setString(11, userProfile.getAbout());
            pstmt.setString(12, userProfile.getFavoriteQuotations());
            pstmt.setString(13, userProfile.getFacebookLink());
            pstmt.setBoolean(14, userProfile.isActive());
            pstmt.setInt(15, userProfile.getIdAccount());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean upadte(UserProfile userProfile) {
        try {
            if(conn == null){
                conn = ConnectionFactory.getConnection();
            }
            String sql = "UPDATE tblUserProfile SET homeTown = ? , school = ? , mobile = ? , email = ? , strength = ? , language = ? , image = ?, about = ?, favoriteQuotations = ? , facebookLink = ? where idUser=" + userProfile.getIdUserProfile();
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1, userProfile.getHomeTown());
            ps.setString(2, userProfile.getSchool());
            ps.setString(3, userProfile.getMobile());
            ps.setString(4, userProfile.getEmail());
            ps.setString(5, userProfile.getStrengths());
            ps.setString(6, userProfile.getLanguages());
            ps.setString(7, userProfile.getImage());
            ps.setString(8, userProfile.getAbout());
            ps.setString(9, userProfile.getFavoriteQuotations());
            ps.setString(10, userProfile.getFacebookLink());

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean delete(int idUserProfile) {
        try {
            String sql = "DELETE tblUserProfile WHERE [idUser] = " + idUserProfile;
            if(conn == null ){
                conn = ConnectionFactory.getConnection();
            }
            
            st = conn.createStatement();
            st.executeUpdate(sql);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }    
    

    @Override
    public UserProfile getUserProfile() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
    
    @Override
    public List<UserProfile> getAll() {
        try {
            conn = ConnectionFactory.getConnection();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserProfileDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UserProfileDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        List list = new ArrayList();
        String sql = "Select [idUser],[fullName],[dateOfBirth],[gender],[homeTown],[school], [mobile] ,[email],[strength], [language] , [image], [about], [favoriteQuotations] , [facebookLink] FROM tblUserProfile";
        
        
        if (conn != null) {
            try {
                st = conn.createStatement();
                rs = st.executeQuery(sql);
                UserProfile entity = null;
                while (rs.next()) {
                    entity = new UserProfile();
                    entity.setIdUserProfile(rs.getInt("idUser"));
                    entity.setFullName(rs.getString("fullName"));
                    entity.setDateOfBirth(rs.getDate("dateOfBirth"));
                    entity.setGender(rs.getBoolean("gender"));
                    entity.setHomeTown(rs.getString("homeTown"));
                    entity.setSchool(rs.getString("school"));
                    entity.setMobile(rs.getString("mobile"));
                    entity.setEmail(rs.getString("email"));
                    entity.setStrengths(rs.getString("strength"));
                    entity.setLanguages(rs.getString("language"));
                    entity.setImage(rs.getString("image"));
                    entity.setAbout(rs.getString("about"));
                    entity.setFavoriteQuotations(rs.getString("favoriteQuotations"));
                    entity.setFacebookLink(rs.getString("facebookLink"));
                    list.add(entity);                    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return list;

    }

    @Override
    public UserProfile findByUserId(int userId) {
        UserProfile entity = null ;
        String sql = "select * from tblUserProfile where idUser=" + userId ;        
        if(conn == null){
            try {
                conn = ConnectionFactory.getConnection();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(UserProfileDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(UserProfileDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try{
        st = conn.createStatement();
        rs = st.executeQuery(sql);
        while(rs.next() ){
          entity = new UserProfile();
                    entity.setIdUserProfile(rs.getInt("idUser"));
                    entity.setFullName(rs.getString("fullName"));
                    entity.setDateOfBirth(rs.getDate("dateOfBirth"));
                    entity.setGender(rs.getBoolean("gender"));
                    entity.setHomeTown(rs.getString("homeTown"));
                    entity.setSchool(rs.getString("school"));
                    entity.setMobile(rs.getString("mobile"));
                    entity.setEmail(rs.getString("email"));
                    entity.setStrengths(rs.getString("strength"));
                    entity.setLanguages(rs.getString("language"));
                    entity.setImage(rs.getString("image"));
                    entity.setAbout(rs.getString("about"));
                    entity.setFavoriteQuotations(rs.getString("favoriteQuotations"));
                    entity.setFacebookLink(rs.getString("facebookLink"));                     
        } 
        
        
        } catch (SQLException ex){
            ex.toString();
        }
        return entity;
        
    }

    @Override
    public UserProfile getUserProfileByID() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
